package com.ait.edu.service.impl;

import com.ait.edu.entity.Subject;
import com.ait.edu.entity.subject.OneSubject;
import com.ait.edu.entity.subject.TwoSubject;
import com.ait.edu.listener.SuobjectExcelListener;
import com.ait.edu.mapper.SubjectMapper;
import com.ait.edu.entity.excel.SubjectData;
import com.ait.edu.service.SubjectService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-10
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;


    //这个是上传一个xlms一个表格  SubjectData是表格的每行数据
    @Override
    public void saveSuobject(MultipartFile file,SubjectService subjectService) {
        try {
            InputStream inputStream = file.getInputStream();
            //这个是一行一行读来触发SuobjectExcelListener拦截器
            EasyExcel.read(inputStream,SubjectData.class,new SuobjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    /*
    * 分装数据
    *
    *  data: [{
          id: 1,
          label: '一级 1',
          children: [{
            id: 4,
            label: '二级 1-1',
          },{
          id: 2,
          label: '一级 2',
          children: [{
            id: 4,
            label: '二级 2-1',
          }]
        },
    *
    * 首先要创建两个实体类OneSubject和twoSubject来装载数据
    *
    * 返回list
    * */
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询所有的一级元素 parentId==0
        QueryWrapper<Subject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<Subject> listOne = this.list(wrapperOne);

        //查询所有的二级元素 parentId!=0

        QueryWrapper<Subject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<Subject> listTwo = this.list(wrapperTwo);

        List<OneSubject> oneSubjectList = new ArrayList<>();
        //封装一级分类

        for (Subject subject : listOne){
            OneSubject oneSubject = new OneSubject();
            oneSubject.setId(subject.getId());
            oneSubject.setLabel(subject.getTitle());
            List<TwoSubject> lists = new ArrayList<>();
            //封装二级分类
            oneSubject.setChildren(lists);
            for (Subject subject1 : listTwo){
                String cdc = subject.getId();
                String cdc1 = subject1.getParentId();
                if (cdc.equals(cdc1)){
                    TwoSubject twoSubject = new TwoSubject();
                    twoSubject.setId(subject1.getId());
                    twoSubject.setLabel(subject1.getTitle());
                    lists.add(twoSubject);

                }
            }

            oneSubjectList.add(oneSubject);
        }

        return oneSubjectList;
    }
}
